<?php
class CategoryModel extends Model{
    protected $icon = array('　┃ ', '　┣━', '　┗━');

    /**
     * 获取module列表
     *
     * @param $module
     */
    public function getModuleList($module)
    {
        $list = $this->field('id,pid,level,name')->order('ordernum asc')->where("module_id='{$module}' and status=1")->select();
        return $list;
    }

    /**
     * 重新生成分类的orderid
     * @param int $pid
     * @param int $orderid
     */
    public function resetorderid($pid = 0, &$orderid = 0)
    {
        $list=(array)$this->field('id,pid,name')->where(array('pid'=>$pid))->order('ordernum asc')->select();
		
        foreach($list as $v){
            $orderid++;
            $this->where(array('id'=>$v['id']))->update(array('orderid'=>$orderid));
            $this->resetorderid($v['id'],$orderid);
        }
    }

    /**
     * 生成对应栏目url
     * @param $id
     * @param int $page
     * @return mixed
     */
    public function geturl($id, $page = 1)
    {
        static $_url;
        $key = $id . '_' . $page;
        if (!isset($_url[$key])) {
            $category_info = dc::get('category',$id);
            if($category_info['module']=='bbs'){
                if ($category_info['level']==1){
                    $_url[$key]=U('bbs.index.index');
                }elseif ($category_info['level']==2){
                    $_url[$key]='javascript:;';
                }else{
                    $_url[$key]=U('bbs.board.index',array('key'=>$category_info['key']));
                }
            }elseif($category_info){
                if ($category_info['level'] == '1') {
                    $_url[$key] = U('index.' . $category_info['module'] . '.list', array('dir' => $category_info['key'], 'page' => $page));
                } else {
                    $paths=explode(',',$category_info['path']);
                    $parent_info = $this->field('module,key')->where("id={$paths['2']}")->find();
                    $_url[$key] = U('index.' . $parent_info['module'] . '.list', array('dir' => $parent_info['key'], 'key' => $category_info['key'], 'page' => $page));
                }
            }else{
                $_url[$key]='';
            }
        }
        return $_url[$key];
    }

    /**
     * 获取分类列表 根据之前设置的参数
     * @return array
     */
    public function getlist()
    {
        $list = (array)$this->select();
        foreach ($list as &$v) {
            $v['url_category'] = $this->geturl($v['id']);
        }
        return $list;
    }

    /**
     * 获取位置信息
     *
     * @param string $id  栏目id
     * @param string $order asc从小到大 desc 从大到小 其他为都有
     * @return mixed
     */
    public function getPosition($id, $order = 'both')
    {
        $info = dc::get('category',$id);
        $position = $this->field('id,key,name')->where("id in (".substr($info['path'],3)."{$info['id']})")->order('orderid asc')->getlist();
        foreach ($position as &$v) {
            $v['url_category'] = $this->geturl($v['id']);
        }
        if ($order == 'asc') {
            return $position;
        } else {
            $res['asc'] = $position;
            krsort($position);
            if ($order == 'desc') {
                return $position;
            } else {
                $res['desc'] = $position;
                return $res;
            }
        }
    }

    /**
     * 无子栏目就获取同级栏目 有子栏目获取下级栏目
     * @param $id
     * @return array
     */
    public function getSiblingList($id)
    {
        $info = dc::get('category',$id);
        $list=$this->where("path like '{$info['path']}{$info['id']},%'")->getlist();
        if (!$list){
            $list=$this->where("pid={$info['pid']}")->getlist();
        }
        return $list;
    }

    /**
     * 获取子栏目列表
     */
    public function getSonId($id)
    {
        $info = dc::get('category',$id);
        if ($info){
            $res= (array)$this->where("path like '{$info['path']}{$info['id']},%'")->getField('id',true);
            $res[]=$id;
            return $res;
        }else{
            return array();
        }
    }
}